
#include <oal_log.h>


static int log_level = OAL_LOG_WARN;

#define MAX_MEM_BLOCK 128

struct oal_log_mem_pool{
	oal_list_t free_list;
	oal_crit_t crit;
	int count;
}mem_pool;
static oal_msgq_t *log_msgq;
static void mem_pool_init(void){
	OAL_LIST_INIT(&mem_pool.free_list);
	oal_crit_init(&mem_pool.crit);
	mem_pool.count = 0;
}
static char *mem_get(void){
	oal_crit_enter(&mem_pool.crit);
	if(!oal_list_empty(&mem_pool.free_list)){
		oal_list_t *free_mem = mem_pool.free_list->next;
		oal_list_del(free_mem);
		oal_crit_leave(&mem_pool.crit);
		return (char *)free_mem;
	}else{
		mem_pool.count++;
		oal_crit_leave(&mem_pool.crit);
		return oal_malloc(OAL_LOG_MAX_LEN);
	}
}
static void mem_put(void *mem){
	oal_list_t *free_mem = (oal_list_t *)mem;
	oal_crit_enter(&mem_pool.crit);
	if(mem_pool.count > MAX_MEM_BLOCK){
		oal_free(mem);
		mem_pool.count--;
	}else{	
		oal_list_add(&mem_pool.free_list,free_mem);	
	}
	oal_crit_leave(&mem_pool.crit);
}
static void log_network_init(int port){

}
OAL_DECL(int) oal_log_init(void)
{
	mem_pool_init();
	oal_msgq = oal_msgq_create(NULL,128);
	log_network_init();
}



OAL_DECL(int) oal_log_write(const char *flie,int line,int level,fmt,...){
	char *mem ;
	if(level < log_level){
		return 0;
	}

	mem = mem_get();

	snprintf(mem,"<%s:%4d> ")
	oal_msgq_add(oal_msgq,mem);
}

OAL_DECL(void) oal_log_set_level(int level);

OAL_DECL(void) oal_log_stdout_enable(void);